/*
 * @Author: curse-the-west m18811101669@163.com
 * @Date: 2022-09-22 15:01:24
 * @LastEditors: nainaiyi s15037075861@163.com
 * @LastEditTime: 2022-10-25 13:46:13
 * @FilePath: /a2001-a_project/websokcot/serve.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const WebSocket = require('ws');

// http:// baidu.com:5000

// ws://baidu.com:6000

const WebSocketServer = WebSocket.Server;


const ws = new WebSocketServer({
    port: 9000
})


// socket 谁连接的信息
let sockets = []; // 接收所有连接ws人
ws.on('connection', (socket) => {
    // 记录每一个连接ws 的人
    sockets.push(socket);

    socket.on('message', (msg) => {
        sockets.forEach(item => {
            // console.log(item.readyState)
            if (item.readyState === 1) {
                // console.log(item,socket)
                // 除了我 其他人都要🉑接收到数据
                if (item !== socket) {
                    item.send(msg.toString('utf-8'))
                }
            }

        })
    })
})